package com.suning.sawp.service.impl.esb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.rsc.dto.responsedto.MbfResponse;
import com.suning.rsc.httpservice.annotation.EsbEIHttpWired;
import com.suning.sawp.dto.esb.ShopFrequencyInfo;
import com.suning.sawp.dto.esb.ShopFrequencyReq;
import com.suning.sawp.dto.esb.ShopFrequencyResp;
import com.suning.sawp.intf.esb.ShopFrequencyEsbService;
import com.suning.sawp.intf.esb.ShopFrequencyService;

/**
 * 
 * 查询会员首购 复购状态<br>
 * 〈功能详细描述〉
 *
 * @author 14062606
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class ShopFrequencyServiceImpl implements ShopFrequencyService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(ShopFrequencyServiceImpl.class);

	private ShopFrequencyEsbService shopFrequencyEsbService;

	public ShopFrequencyEsbService getShopFrequencyEsbService() {
		return shopFrequencyEsbService;
	}

	@EsbEIHttpWired
	public void setShopFrequencyEsbService(
			ShopFrequencyEsbService shopFrequencyEsbService) {
		this.shopFrequencyEsbService = shopFrequencyEsbService;
	}

	@Override
	public Map<String, String> queryShopFrequency(List<String> custList) {
		ShopFrequencyReq req = new ShopFrequencyReq();
		Map<String, String> shopFreMap = new HashMap<String, String>();
		if (CollectionUtils.isEmpty(custList)) {
		    return shopFreMap;
		}
		List<String> custNumList = new ArrayList<String>();
		for (String custNum : custList) {
			custNumList.add(custNum);
		}
		req.setCustNum(custNumList);

		MbfResponse response = null;
		try {
			response = shopFrequencyEsbService.queryShopFrequency(req);
		} catch (Exception e) {
			LOGGER.error("查询首购复购标记接口异常", e);
		}
		
		if (null != response && null != response.getOutput()) {
		    ShopFrequencyResp resp = (ShopFrequencyResp) response.getOutput()
	                .getMbfBody(ShopFrequencyResp.class);
	        if (resp != null) {
	            List<ShopFrequencyInfo> shopFreList = resp.getCustList();
	            if (CollectionUtils.isNotEmpty(shopFreList)){
	                for (ShopFrequencyInfo shopFreInfo : shopFreList) {
	                    String custNum = shopFreInfo.getCustNum();
	                    String shopFrequency = shopFreInfo.getShopFrequency();
	                    shopFreMap.put(custNum, shopFrequency);
	                }
	            }
	        }
		}
		
		return shopFreMap;
	}
}
